package com.hiray.calendar;

import java.util.Calendar;

/**
 * Created by hiray on 2018/2/26.
 *
 * @author hiray
 */

public class CalendarUtil {


    public static int getMonthDays(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, 1);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    public static int year() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    public static int month() {
        return Calendar.getInstance().get(Calendar.MONTH);
    }

    /**
     * @return day of month
     */
    public static int dayOfMonth() {
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    }

    /**
     * @return day of week
     * 1:Sunday
     * 2:Monday
     * 3:Tuesday
     * 4:Wednesday
     * 5:Thursday
     * 6:Friday
     * 7:Saturday
     */
    public static int dayOfWeek() {
        return Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
    }

    /**
     * @return the number of day in specific month
     */
    public static int daysOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, 1);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * @param month
     * @return how many days in specific month of this year
     */
    public static int daysOfMonth(int month) {
        return daysOfMonth(year(), month);
    }

    /**
     * @return what day is the first day of this month
     */
    public static int firstDayOfWeek() {
        return firstDayOfWeek(year(), month());
    }

    /**
     * @return what day is the first day of certain month of certain year
     */
    public static int firstDayOfWeek(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, 1);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }

    public static final int SSS = 1;

    /**
     * synonym for {@link #dayOfMonth()}
     */
    public static int date() {
        return dayOfMonth();
    }

    public static boolean isToday(int year, int month, int date) {
        return year() == year &&
                month() == month &&
                date() == date;
    }

    /**
     * @param year  year
     * @param month month
     * @return how many rows there is in this page of month
     */
    public static int rowsOfMonth(int year, int month) {
        int day = 8 - firstDayOfWeek(year, month);
        int daysOfMonth = CalendarUtil.daysOfMonth(year, month);
        int r = (daysOfMonth - day) / 7 + 1;
        if ((daysOfMonth - day) % 7 == 0)
            return r;
        else
            return r + 1;

    }
}
